//
//  LZSHistoryViewController.m
//  IOSProject
//
//  Created by Mac on 2018/4/18.
//  Copyright © 2018年 com.linzhisheng. All rights reserved.
//

#import "LZSHistoryViewController.h"

#import "LZSTypeTableViewCell.h"
#import "LZSTypeModel.h"

#import "LZSTypeViewController.h"

#import "LZSCaiPiaoClassModel.h"

#import "SXTLoginViewController.h"

@interface LZSHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *realArray;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) LZSNoDataView *noview;
@end

@implementation LZSHistoryViewController

-(NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray =  [NSMutableArray arrayWithObjects:@{@"lottery_id": @"ssq", @"lottery_name": @"双色球",@"lottery_type_id": @"1",@"remarks": @"每周二、四、日开奖"},@{
                                                                                                                                                             @"lottery_id": @"dlt",
                                                                                                                                                             @"lottery_name": @"超级大乐透",
                                                                                                                                                             @"lottery_type_id": @"2",
                                                                                                                                                             @"remarks": @"每周一、三、六开奖"
                                                                                                                                                             }, @{
                                                                                                                                                                  @"lottery_id": @"qlc",
                                                                                                                                                                  @"lottery_name": @"七乐彩",
                                                                                                                                                                  @"lottery_type_id": @"1",
                                                                                                                                                                  @"remarks": @"每周一、三、五开奖"
                                                                                                                                                                  },
                       @{
                         @"lottery_id": @"fcsd",
                         @"lottery_name": @"福彩3D",
                         @"lottery_type_id": @"1",
                         @"remarks": @"每日开奖"
                         },
                       @{
                         @"lottery_id": @"qxc",
                         @"lottery_name": @"七星彩",
                         @"lottery_type_id": @"2",
                         @"remarks": @"每周二、五、日开奖"
                         },
                       @{
                         @"lottery_id": @"pls",
                         @"lottery_name": @"排列3",
                         @"lottery_type_id": @"2",
                         @"remarks": @"每日开奖"
                         },
                       @{
                         @"lottery_id": @"plw",
                         @"lottery_name": @"排列5",
                         @"lottery_type_id":@"2",
                         @"remarks": @"每日开奖"
                         },
                       nil];
    }
    return _dataArray;
}

-(NSMutableArray *)realArray{
    if (!_realArray) {
        _realArray = [NSMutableArray array];
    }
    return _realArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor yellowColor];
    
//    self.realArray = [LZSTypeModel mj_objectArrayWithKeyValuesArray:self.dataArray];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor lightGrayColor];
    //
    self.tableView.estimatedRowHeight = 80;
    
    self.tableView.separatorColor = [UIColor colorWithHexString:@"#c8c8c8"];
    
    //默认【下拉刷新】
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    //默认【上拉加载】
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    //
    self.tableView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(0);
    }];
    
    LZSNoDataView *nodataView = [[LZSNoDataView alloc] init];
    self.noview = nodataView;
    [self.tableView addSubview:nodataView];
    nodataView.hidden = YES;
    [nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView.mas_centerX);
        make.centerY.equalTo(self.tableView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    //    [self headerSdcycleScrollView];
    
    //登录成功通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"LOGINSUCCESS" object:nil];
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginOUT) name:@"LOGOUT" object:nil];
    
//    [self lotterType];
//    [self caipiaoclass];
}

-(void)loginOUT{
    
    [self.realArray removeAllObjects];
    
    [self refresh];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.tableView.mj_header beginRefreshing];
}

-(void)refresh{
    //    sleep(0.5);
    self.page = 1;

    [self caipiaoclass];
}

-(void)loadMore{
    [self caipiaoclass];
}

//-(void)lotterType{
//    
//    NSDictionary *loginDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
//    if (loginDic.count) {
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setValue:@"91b22ffb918dbb3a2b5f98be27009a47" forKey:@"key"];
//    [self postData:@"http://apis.juhe.cn/lottery/types" param:params success:^(id responseObject) {
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
//
////        NSLog(@"%@",responseObject);
//        [self.realArray removeAllObjects];
//        
//        NSArray *dataArray = [LZSTypeModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
////    NSArray *dataArray = [LZSTypeModel mj_objectArrayWithKeyValuesArray:self.dataArray];
//    
//        if (dataArray.count<20) {
//            self.tableView.mj_footer.hidden = YES;
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
//        
//        [self.realArray addObjectsFromArray:dataArray];
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView reloadData];
//        });
//    } error:^(NSError *error) {
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
//        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
//        NSLog(@"%@",error);
//    }];
//    }else{
//        
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
//        //
////        self.tableView.hidden = YES;
//        self.noview.hidden = NO;
//    }
//}

-(void)caipiaoclass{
    NSDictionary *loginDic = [[NSUserDefaults standardUserDefaults]valueForKey:@"ISLOGIN"];
    if (loginDic.count) {
        self.noview.hidden = YES;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"31771dfbcb6cda3d" forKey:@"appkey"];
    [params setValue:[NSString stringWithFormat:@"%ld",(long)self.page] forKey:@"page"];
    [self getData:@"http://api.jisuapi.com/caipiao/class" param:params success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
//        [self.realArray removeAllObjects];
        
        NSArray *dataArray = [LZSCaiPiaoClassModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
        //    NSArray *dataArray = [LZSTypeModel mj_objectArrayWithKeyValuesArray:self.dataArray];
        
        //        //是刷新还是加载
        if (self.page==1) {
            if (dataArray.count==0) {
//                self.nodataView.hidden = NO;
                self.tableView.mj_footer.hidden = YES;
            }else{
//                self.nodataView.hidden = YES;
                self.tableView.mj_footer.hidden = NO;
            }
            
            [self.realArray removeAllObjects];
            [self.realArray addObjectsFromArray:dataArray];
        }else{
            [self.realArray addObjectsFromArray:dataArray];
        }
        
        //是否提示可加载
        if (dataArray.count<20) {
            // 变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else{
            self.page++;
        }
        
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.view makeToast:error.domain duration:0.8 position:CSToastPositionCenter];
        NSLog(@"%@",error);
    }];
    }else{
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self.noview.hidden = NO;
        
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self.tableView reloadData];
        });
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"亲,您还未登录,暂时无法使用该功能~" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okaction = [UIAlertAction actionWithTitle:@"去登陆" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SXTLoginViewController *loginVc = [[SXTLoginViewController alloc] init];
            [self.navigationController pushViewController:loginVc animated:YES];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertVC addAction:okaction];
        [alertVC addAction:cancelAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.realArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LZSTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[LZSTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
//    cell.model = self.realArray[indexPath.row];
    //    cell.timeLabel.text = @"个人信息的描述个人信息的描述";
    //    [cell.btn setTitle:@"关注" forState:UIControlStateNormal];
    cell.classModel = self.realArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    LZSTypeModel *model = self.realArray[indexPath.row];
    LZSCaiPiaoClassModel *model = self.realArray[indexPath.row];
    LZSTypeViewController *typeVC = [[LZSTypeViewController alloc] init];
//    typeVC.lottery_id = model.lottery_id;
    typeVC.caipiaoid = model.caipiaoid;
    [self.navigationController pushViewController:typeVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
